package com.principle.ocp;

public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}
//这是用于绘图的类
class GraphicEditor{
    //无论新增加什么功能方法，对于使用方来说是不用修改代码
    public void drawShape(Shape s){
        s.draw();
    }
}
//基类
abstract class Shape{
    int m_type;
    public abstract void draw();
}
class Rectangle extends Shape{
    Rectangle(){
        super.m_type=1;
    }
    @Override
    public void draw() {
        System.out.println("矩形");
    }
}
class Circle extends Shape{
    Circle(){
        super.m_type=2;
    }
    @Override
    public void draw() {
        System.out.println("圆形");
    }
}
//新增加画三角形
class Triangle extends Shape{
    Triangle(){
        super.m_type=3;
    }

    @Override
    public void draw() {
        System.out.println("三角形");
    }
}